package com.xydtech.ecard.product.controller;

import com.xydtech.common.config.XydtechBusiPlatConfig;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.file.FileUploadUtils;
import com.xydtech.common.utils.file.MimeTypeUtils;
import com.xydtech.ecard.ecard.controller.EcardApiBaseController;
import com.xydtech.ecard.product.domain.EcardPrdDefinedSkip;
import com.xydtech.ecard.product.service.IEcardPrdDefinedSkipService;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import com.xydtech.system.domain.SysWechatApplet;
import com.xydtech.system.service.ISysWechatAppletService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
 * 产品自定义跳转配置Controller
 *
 * @author Daniel
 * @date 2022-05-07
 */
@RestController
@Api(tags = "电子名片-产品配置-产品自定义跳转配置")
@RequestMapping("/ecardSaasApi/product/prdDefinedSkip")
public class EcardPrdDefinedSkipController extends EcardApiBaseController {

    @Autowired
    private IEcardPrdDefinedSkipService ecardPrdDefinedSkipService;
    @Autowired
    private ISysWechatAppletService wechatAppletService;
    @Autowired
    private IEcardApiUserService ecardApiUserService;
    //文件分布式存储开关
    @Value("${busiplat.minioUpload}")
    private boolean minioUpload;

    /**
     * 电子名片-设置-产品配置-获取产品自定义跳转配置表
     * @param ecardPrdDefinedSkip
     * @return com.xydtech.common.model.ResponseModel
     * @exception
     * @author Daniel
     * @date 2022/5/9 9:20
     */
    @GetMapping("/selectPrdDefinedSkipList")
    @ApiOperation(value = "电子名片-设置-产品配置-获取产品自定义跳转配置表", notes = "电子名片-设置-产品配置-获取产品自定义跳转配置表")
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = ResponseModel.class)})
    @ResponseBody
    public ResponseModel selectPrdDefinedSkipList(EcardPrdDefinedSkip ecardPrdDefinedSkip) {
        startPage();
        String currentUserId = getCurrentUserId();
        ecardPrdDefinedSkip.setRegistId(Long.parseLong(currentUserId));
        List<EcardPrdDefinedSkip> list = ecardPrdDefinedSkipService.selectEcardPrdDefinedSkipList(ecardPrdDefinedSkip);
        return getTableResponse(list);
    }

    /**
     * 电子名片-设置-产品配置-新增产品自定义跳转配置-上传二维码
     * @param file
     * @return
     */
    @PostMapping("/uploadPrdQrcode")
    @ApiOperation(value = "电子名片-设置-产品配置-新增产品自定义跳转配置-上传二维码", notes = "电子名片-设置-产品配置-新增产品自定义跳转配置-上传二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qrcode", value = "产品二维码文件", dataType = "MultipartFile", dataTypeClass = MultipartFile.class, paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "产品自定义跳转配置ID", dataType = "Long", dataTypeClass = Long.class, paramType = "body", required = true),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = ResponseModel.class)})
    @ResponseBody
    public ResponseModel uploadPrdQrcode(@RequestParam("qrcode") MultipartFile file, Long id) {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());
            //当前小程序对应的法人机构ID
            Long parentDeptId = wechatApplet.getDeptId();

            if (!file.isEmpty()) {
                //20230711 update 加入图片文件类型后缀判断
                String prdQrcodeFilePath = FileUploadUtils.uploadFileType(
                        XydtechBusiPlatConfig.getPrdDefinedSkipQrcodePath(parentDeptId), file, minioUpload, MimeTypeUtils.IMAGE_EXTENSION);

                //如果自定义跳转的id不为空，则更新下图片的地址，如果为空，说明是新增的配置，只需要返回上传图片的地址即可
                if (!Objects.isNull(id)) {
                    EcardPrdDefinedSkip ecardPrdDefinedSkip = new EcardPrdDefinedSkip();
                    ecardPrdDefinedSkip.setId(id);
                    ecardPrdDefinedSkip.setRegistId(Long.parseLong(currentUserId));
                    ecardPrdDefinedSkip.setImgUrl(prdQrcodeFilePath);
                    ecardPrdDefinedSkipService.updatePrdQrcode(ecardPrdDefinedSkip);
                }
                return ResponseModel.ok(prdQrcodeFilePath);
            } else {
                return ResponseModel.error("头像文件不能为空！");
            }
        } catch (Exception e) {
            logger.error("上传头像失败！", e);
            return ResponseModel.error(e.getMessage());
        }
    }

    /**
     * 电子名片-设置-产品配置-保存产品自定义跳转配置
     * @param ecardPrdDefinedSkip
     * @return com.xydtech.common.model.ResponseModel
     * @exception
     * @author Daniel
     * @date 2022/5/9 10:06
     */
    @PostMapping("/savePrdDefinedSkip")
    @ApiOperation(value = "电子名片-设置-产品配置-保存产品自定义跳转配置", notes = "电子名片-设置-产品配置-保存产品自定义跳转配置")
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = ResponseModel.class)})
    @ResponseBody
    public ResponseModel savePrdDefinedSkip(@RequestBody EcardPrdDefinedSkip ecardPrdDefinedSkip) {
        /**
         * 不管是新增或者更新，肯定都是要弄自己的产品自定义跳转配置，因此regist_id都是当前登录用户
         */
        String currentUserId = getCurrentUserId();
        ecardPrdDefinedSkip.setRegistId(Long.parseLong(currentUserId));
        int saveCount = ecardPrdDefinedSkipService.savePrdDefinedSkip(ecardPrdDefinedSkip);
        return saveCount > 0 ? ResponseModel.ok(ecardPrdDefinedSkip) : ResponseModel.error("保存失败！");
    }

    /**
     * 电子名片-设置-产品配置-获取产品自定义跳转配置
     */
    @GetMapping("/getPrdDefinedSkipInfo/{id}")
    @ApiOperation(value = "电子名片-设置-产品配置-获取产品自定义跳转配置", notes = "电子名片-设置-产品配置-获取产品自定义跳转配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "自定义跳转配置ID", dataType = "Long", dataTypeClass = Long.class, paramType = "body", required = true)
    })
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = EcardPrdDefinedSkip.class)})
    @ResponseBody
    public ResponseModel getPrdDefinedSkipInfo(@PathVariable("id") Long id) {
        String currentUserId = getCurrentUserId();
        EcardPrdDefinedSkip ecardPrdDefinedSkip = ecardPrdDefinedSkipService.getPrdDefinedSkipInfo(id, currentUserId);
        return ResponseModel.ok(ecardPrdDefinedSkip);
    }

    /**
     * 电子名片-设置-产品配置-删除产品自定义跳转配置
     */
    @PostMapping("/deletePrdDefinedSkip/{id}")
    @ApiOperation(value = "电子名片-设置-产品配置-删除产品自定义跳转配置", notes = "电子名片-设置-产品配置-删除产品自定义跳转配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "自定义跳转配置ID", dataType = "Long", dataTypeClass = Long.class, paramType = "body", required = true)
    })
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = EcardPrdDefinedSkip.class)})
    @ResponseBody
    public ResponseModel deleteEcardPrdDefinedSkip(@PathVariable("id") Long id) {
        String currentUserId = getCurrentUserId();
        int delCount = ecardPrdDefinedSkipService.deletePrdDefinedSkip(id, currentUserId);
        return delCount > 0 ? ResponseModel.ok() : ResponseModel.error("删除失败！");
    }
}
